package soen6441;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import java.util.logging.Logger;

class EmailAddressImpl implements EmailAddress {
    static Logger logger
            = Logger.getLogger(EmailAddressImpl.class.getName());
    String userName;
    String domain;

    EmailAddressImpl(String userName, String domain) {
        this.userName = userName;
        this.domain = domain;
    }

    public void sendMessage(String subject, String body){
        try {
            trySendMessage(subject, body);
        } catch(EmailException e){

        }
    }

    private void trySendMessage(String subject, String body)
            throws EmailException {
        // commons email and java mail api
        SimpleEmail email = new SimpleEmail();
        email.setHostName("smtp.gmail.com");
        email.setSmtpPort(587);
        email.setAuthenticator( MyCredentials.authenticator() );
        email.setTLS(true);
        email.addTo(userName + "@" + domain, userName);
        email.setFrom(MyCredentials.email(), MyCredentials.name());
        email.setSubject(subject);
        email.setMsg(body);
        email.send();
    }


}
